<?php
class Shop_Widgets_Menu_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $lang = $request->getParam('lang');
		
        //Get connection to database
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();		
		
        //Category models access to data
        $categoryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getCategoryDao();
        $categoryDao->setDbConnection($conn);
        $categoryDao->setLang($lang);
		
		//Get category tree
        $categories = $categoryDao->getFTree();

        $str = "  <ul>\r\n";
        $currenLevel = 0;
        $first = true;
        foreach ($categories as $row) {
            $name = ($row->category_content_name)?$row->category_content_name:$row->category_admin_name;
            $arrRoute = array(
                'cat' => $row->category_id,
                'slug' => ($row->slug)?$row->slug:Tomato_Utility_String::removeSign($row->category_admin_name, '-', true)
            );
            if ($row->depth >= 0 && $row->depth <= 1) {
                if ($row->depth <= $currenLevel && !$first) {
                    $str .= "</li>\r\n";
                }
                if ($row->depth < $currenLevel && !$first) {
                    for ($x = 0; $x < $currenLevel - $row->depth; $x++) {
                        $str .= "</ul></li>\r\n";
                    }
                }
                if ($row->depth > $currenLevel && !$first) {
                    $str .= "<ul class='level{$row->depth}'>\r\n";
                }
                $str .= "<li><a href='".$this->_view->url($arrRoute, 'shop_product_all_categories')."'>" . $name . "</a>\r\n";

                $currenLevel = $row->depth;
                $first = false;
            }
        }
        if (!$first) {
            $str .= "</li>";
            for ($x = 1; $x < $currenLevel; $x++) {
                $str .= "</ul></li>\r\n";
            }
        }
        $str .= "</ul>";
        $this->_view->assign('strMenu', $str);
    }
}
